package com.example.wecompose.ui

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.wecompose.R
import com.example.wecompose.WeViewModel
import com.example.wecompose.ui.theme.WeComposeTheme

@Composable
fun DiscoveryList(viewModel: WeViewModel) {
    DiscoveryListContent(viewModel)
}

@Composable
private fun DiscoveryListContent(viewModel: WeViewModel) {
    Column(Modifier
        .fillMaxSize()
        .background(WeComposeTheme.colors.background)) {

        WeTopBar("发现",viewModel)

        DiscoveryItem("朋友圈",R.drawable.ic_gongzong,R.drawable.icon3)
        Spacer(Modifier.height(10.dp))
        DiscoveryItem("视频号", R.drawable.ic_video_channel, R.drawable.icon1, true)
        Spacer(Modifier.height(10.dp))

        DiscoveryItem("看一看", R.drawable.ic_kyk)
        HorizontalDivider(Modifier.padding(start = 60.dp), thickness = 0.8f.dp)
        DiscoveryItem("搜一搜", R.drawable.ic_sys)
        Spacer(Modifier.height(10.dp))

        DiscoveryItem("直播和附近", R.drawable.ic_live)


    }
}

@Composable
private fun DiscoveryItem(title: String, @DrawableRes discoveryIcon: Int, @DrawableRes isIcon: Int? = null, islike: Boolean?=null) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .background(WeComposeTheme.colors.listItem)
            .fillMaxWidth()
            .padding(10.dp)
            .clickable() {

            }
    ) {
        Image(
            painterResource(discoveryIcon),
            null,
            Modifier
                .padding(4.dp)
                .size(24.dp)
                .clip(RoundedCornerShape(4.dp))
        )
        Spacer(Modifier.width(10.dp))

        Text(
            title, fontSize = 17.sp, color = WeComposeTheme.colors.textPrimary
        )

        Spacer(Modifier.weight(1f))

        if (isIcon != null) {
            Image(
                painterResource(isIcon),
                null,
                Modifier.size(24.dp)
            )
        }

        if (islike != null) {
            Text(
                "赞过",
                fontSize = 14.sp,
                color = WeComposeTheme.colors.textSecondary,
                modifier = Modifier.padding(start = 8.dp)
            )
        }

        Icon(
            painterResource(R.drawable.ic_arrow),
            null,
            Modifier
                .align(Alignment.CenterVertically)
                .size(36.dp)
                .padding(8.dp),
            tint = WeComposeTheme.colors.icon
        )

    }
}

@Preview
@Composable
private fun DiscoveryPreview() {
    val viewModel: WeViewModel = viewModel()
    DiscoveryListContent(viewModel)
}